package com.ruoyi.system.listener;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.constant.WorkFlowConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.ApproveCarbonCopy;
import com.ruoyi.system.domain.dto.json.ChildNode;
import com.ruoyi.system.domain.dto.json.UserInfo;
import com.ruoyi.system.service.IApproveCarbonCopyService;
import com.ruoyi.system.utils.BpmnModelUtils;
import com.ruoyi.system.utils.SpringContextHolder;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;



/**
 * 抄送监听器
 */
@Component("ccListener")
public class CcListener implements JavaDelegate {

    @Resource
    private RepositoryService repositoryService;
    @Autowired
    private IApproveCarbonCopyService ccService;

    @Override
    public void execute(DelegateExecution execution) {
        String currentActivityId = execution.getCurrentActivityId();
        //读取流程变量值
        List<String> selectUsers = (List<String>) execution.getVariable(currentActivityId);
        List<ApproveCarbonCopy> ccs= new ArrayList<>();
        //流程抄送人信息保存至数据库
        if(CollUtil.isNotEmpty(selectUsers)){

            for (String selectUser : selectUsers) {
                ApproveCarbonCopy cc = new ApproveCarbonCopy();
                String date = DateUtils.dateTimeNow();
                String id = date + IdUtils.fastSimpleEnglishUUID(5);
                cc.setId(id);
                cc.setUserId(Long.valueOf(selectUser));
                cc.setProcessInstanceId(execution.getProcessInstanceId());
                ccs.add(cc);
            }
        }

        ChildNode childNode = getNode(execution);
        ChildNode node = BpmnModelUtils.getChildNode(childNode, execution.getCurrentActivityId());
        List<UserInfo> value = node.getProps().getAssignedUser();

        if(value!=null){
            for (UserInfo userInfo : value) {
                if(userInfo.getType().equals("user")){
                    ApproveCarbonCopy cc = new ApproveCarbonCopy();
                    String date = DateUtils.dateTimeNow();
                    String id = date + IdUtils.fastSimpleEnglishUUID(5);
                    cc.setId(id);
                    cc.setUserId(Long.valueOf(userInfo.getId()));
                    cc.setProcessInstanceId(execution.getProcessInstanceId());
                    ccs.add(cc);
                }
            }
        }

        if(CollUtil.isNotEmpty(ccs)){
            for(ApproveCarbonCopy approveCarbonCopy: ccs){
                ccService.insertApproveCarbonCopy(approveCarbonCopy);
            }
        }
    }


    private ChildNode getNode(DelegateExecution execution) {

        Process mainProcess = repositoryService.getBpmnModel(execution.getProcessDefinitionId()).getMainProcess();
        String dingDing = mainProcess.getAttributeValue(WorkFlowConstants.FLOWABLE_NAME_SPACE, WorkFlowConstants.FLOWABLE_NAME_SPACE_NAME);
        JSONObject jsonObject = JSONObject.parseObject(dingDing, new TypeReference<JSONObject>() {
        });
        String processJson = jsonObject.getString(WorkFlowConstants.VIEW_PROCESS_JSON_NAME);
        ChildNode childNode = JSONObject.parseObject(processJson, new TypeReference<ChildNode>(){});
        return childNode;
    }
}
